/**
 * Author: Todd Kerpelman
 * Date: 6/14/12
 * Time: 3:36 PM
 *
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package net.kerp.hangoutSpace {

import net.kerp.hangoutSpace.Events.HangoutEvent;
import flash.events.EventDispatcher;

public class PeopleManager extends EventDispatcher {

  // TODO: Make this a vector of HangoutParticipants
  private var _peopleList:Array;

  public function PeopleManager() {
    _peopleList = [];
  }

  private function getUserById(idToCheck:String):HangoutParticipant
  {
    for each (var checkMe:HangoutParticipant in _peopleList) {
      if (checkMe.id == idToCheck) {
        return checkMe;
      }
    }
    return null;
  }

  private function getUserLocInArray(idToCheck:String):int
  {
    for (var i:int =0; i<_peopleList.length; i++) {
      if (_peopleList[i].id == idToCheck) {
        return i;
      }
    }
    return -1;
 }

  // In general, this is just used when we're initially getting into the
  // app.
  public function setAllUsers(arrayOfUserObjects:Array):void
  {
    if (_peopleList.length > 0) {
      trace("Hmmm... why am I getting an allUsers request now?");
    } else {
      for each (var addMe:Object in arrayOfUserObjects) {
        addNewFloatyPersonFromHangoutObject(addMe);
      }
    }

  }

  private function addNewFloatyPersonFromHangoutObject(addMe:Object):void {
    var newParticipant:HangoutParticipant = new HangoutParticipant();
    trace("Getting ready to add a new participant...");
    trace("Adding " + addMe.person.displayName);
    newParticipant.displayName = addMe.person.displayName;
    trace("ID " + addMe.id);
    newParticipant.id = addMe.id;
    trace("ImageU " + addMe.person.image);
    newParticipant.imageUrl = addMe.person.image.url;
    trace("ImageUrl " + addMe.person.image.url);
    _peopleList.push(newParticipant);
    dispatchEvent(new HangoutEvent(HangoutEvent.PARTICIPANT_ADDED, newParticipant, true));

  }

  public function addNewUser(userObject:Object):void {
    if (getUserById(userObject.id) != null) {
      trace("Why am I being asked to add a user that's already here?");
    } else {
      addNewFloatyPersonFromHangoutObject(userObject);
    }
  }
  public function removeExistingUser(userObject:Object):void {
    var removeMeIndex:int = getUserLocInArray(userObject.id);
    if (removeMeIndex > -1) {
      var participantToRemove:HangoutParticipant = _peopleList[removeMeIndex];
      _peopleList.splice(removeMeIndex, 1);
      dispatchEvent(new HangoutEvent(HangoutEvent.PARTICIPANT_REMOVED, participantToRemove, true));
    }

  }

  public function processVolumeData(data:Object):void {
    var allVolumes:Object = data.volumes;
    for (var userId:String in allVolumes) {
      var userToApply:HangoutParticipant = getUserById(userId)
      if (userToApply != null) {
        userToApply.volume = int(allVolumes[userId]);
      }
    }
    
  }
}
}
